/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.entities.area;

import rpg.Location;
import rpg.RPG;
import rpg.maps.Map;
import rpg.maps.MapTile;

/**
 *
 * @author rothens
 */
  public class Renderable {

        public MapTile[] tiles;
        public int width;
        public int height;
        public Location topLeft;
        public Location bottomRight;

        public Renderable(Map map, int cX, int cY) {
            topLeft = new Location(cX - RPG.WINDOW_WIDTH / 2, cY - RPG.WINDOW_HEIGHT / 2, false, map);
            bottomRight = new Location(cX + RPG.WINDOW_WIDTH / 2 - 1, cY + RPG.WINDOW_HEIGHT / 2 - 1, false, map);
            width = bottomRight.getTileX() - topLeft.getTileX() + 1;
            height = bottomRight.getTileY() - topLeft.getTileY() + 1;
            getTiles(map);
        }

        private void getTiles(Map map) {
            tiles = new MapTile[width * height];
            int start = topLeft.getMapCoordinate();
            for (int i = 0; i < width * height; i++) {
                int val = start + i % width + map.getWidth() * (i / width);

                if(val>=0) tiles[i] = map.tiles[val];
            }
        }

        public boolean isInside(Location l) {
            return (l.getTileX() >= topLeft.getTileX() && l.getTileY() >= topLeft.getTileY()
                    && l.getTileX() <= bottomRight.getTileX() && l.getTileY() <= bottomRight.getTileY());
        }
    }
